package com.fysports.web.kit.commons;

import com.fysports.web.model.system.SystemDict;

import java.util.Date;
import java.util.UUID;


/**
 * 工具类 - 编号生成
 * 
 */

public class SerialNumberUtil {
	
	public static final String PRODUCT_SN_PREFIX = "SN_";// 货号前缀
	
	public static final String ORDER_SN_PREFIX = "";// 订单编号前缀
	public static final long ORDER_SN_FIRST = 100000L;// 订单编号起始数
	public static final long ORDER_SN_STEP = 1L;// 订单编号步长
	
	public static final String PAYMENT_SN_PREFIX = "";// 支付编号前缀
	public static final long PAYMENT_SN_FIRST = 100000L;// 支付编号起始数
	public static final long PAYMENT_SN_STEP = 1L;// 支付编号步长
	
	public static final String REFUND_SN_PREFIX = "";// 退款编号前缀
	public static final long REFUND_SN_FIRST = 100000L;// 退款编号起始数
	public static final long REFUND_SN_STEP = 1L;// 退款编号步长
	
	public static final String SHIPPING_SN_PREFIX = "";// 发货编号前缀
	public static final long SHIPPING_SN_FIRST = 100000L;// 发货编号起始数
	public static final long SHIPPING_SN_STEP = 1L;// 发货编号步长
	
	public static final String RESHIP_SN_PREFIX = "";// 退货编号前缀
	public static final long RESHIP_SN_FIRST = 100000L;// 退货编号起始数
	public static final long RESHIP_SN_STEP = 1L;// 退货编号步长
	
	public static Long lastOrderSnNumber;
	public static Long lastPaymentSnNumber;
	public static Long lastRefundSnNumber;
	public static Long lastShippingSnNumber;
	public static Long lastReshipSnNumber;
	public static Long lastTenpayTransactionIdNumber;

	{
		lastSnNumberInit();
	}
	public static Long getPrefix(){
		return Long.valueOf(DateTimeKit.format(new Date(),"yyyyHHmm"));
	}

	public static void lastSnNumberInit(){
		// 订单编号
		String lastOrderSn = SystemDict.dao.getLastOrderSn();
		if (!StringKit.isBlank(lastOrderSn)) {
			lastOrderSnNumber = getPrefix()+Long.valueOf(lastOrderSn);
		} else {
			lastOrderSnNumber = ORDER_SN_FIRST;
		}

		// 支付编号
//		String lastPaymentSn = Payment.dao.getLastPaymentSn();
//		if (StringUtils.isNotEmpty(lastPaymentSn)) {
//			lastPaymentSnNumber = Long
//					.parseLong(StringUtils.removeStartIgnoreCase(lastPaymentSn, PAYMENT_SN_PREFIX));
//		} else {
			lastPaymentSnNumber = PAYMENT_SN_FIRST;
//		}

		// 退款编号
//		String lastRefundSn = Refund.dao.getLastRefundSn();
//		if (StringUtils.isNotEmpty(lastRefundSn)) {
//			lastRefundSnNumber = Long
//					.parseLong(StringUtils.removeStartIgnoreCase(lastRefundSn, REFUND_SN_PREFIX));
//		} else {
			lastRefundSnNumber = REFUND_SN_FIRST;
//		}

		// 发货编号
//		String lastShippingSn = Shipping.dao.getLastShippingSn();
//		if (StringUtils.isNotEmpty(lastShippingSn)) {
//			lastShippingSnNumber = Long
//					.parseLong(StringUtils.removeStartIgnoreCase(lastShippingSn, SHIPPING_SN_PREFIX));
//		} else {
			lastShippingSnNumber = SHIPPING_SN_FIRST;
//		}

		// 退货编号
//		String lastReshipSn = Reship.dao.getLastReshipSn();
//		if (StringUtils.isNotEmpty(lastReshipSn)) {
//			lastReshipSnNumber = Long
//					.parseLong(StringUtils.removeStartIgnoreCase(lastReshipSn, RESHIP_SN_PREFIX));
//		} else {
			lastReshipSnNumber = RESHIP_SN_FIRST;
//		}
	}
		
	
	/**
	 * 生成货号
	 * 
	 * @return 货号
	 */
	public static String buildProductSn() {
		String uuid = UUID.randomUUID().toString();
		return PRODUCT_SN_PREFIX + (uuid.substring(0, 8) + uuid.substring(9, 13)).toUpperCase();
	}
	
	/**
	 * 生成订单编号
	 * 
	 * @return 订单编号
	 */
	public synchronized static String buildOrderSn() {
		lastOrderSnNumber += ORDER_SN_STEP;
		return ORDER_SN_PREFIX + lastOrderSnNumber;
	}
	
//	/**
//	 * 生成支付编号
//	 *
//	 * @return 支付编号
//	 */
//	public synchronized static String buildPaymentSn() {
//		lastPaymentSnNumber += PAYMENT_SN_STEP;
//		return PAYMENT_SN_PREFIX + lastPaymentSnNumber;
//	}
//
//	/**
//	 * 生成退款编号
//	 *
//	 * @return 退款编号
//	 */
//	public synchronized static String buildRefundSn() {
//		lastRefundSnNumber += REFUND_SN_STEP;
//		return REFUND_SN_PREFIX + lastRefundSnNumber;
//	}
//
//	/**
//	 * 生成发货编号
//	 *
//	 * @return 发货编号
//	 */
//	public synchronized static String buildShippingSn() {
//		lastShippingSnNumber += SHIPPING_SN_STEP;
//		return SHIPPING_SN_PREFIX + lastShippingSnNumber;
//	}
//
//	/**
//	 * 生成退货编号
//	 *
//	 * @return 退货编号
//	 */
//	public synchronized static String buildReshipSn() {
//		lastReshipSnNumber += RESHIP_SN_STEP;
//		return RESHIP_SN_PREFIX + lastReshipSnNumber;
//	}


	public static void main(String[] args) {
		SerialNumberUtil.lastSnNumberInit();
		System.out.println(SerialNumberUtil.buildProductSn());
		System.out.println(SerialNumberUtil.buildOrderSn());
//		System.out.println(SerialNumberUtil.buildRefundSn());
//		System.out.println(SerialNumberUtil.buildShippingSn());
//		System.out.println(SerialNumberUtil.buildReshipSn());
	}

}